package com.itqf.Controller;

import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Note.NoteService;
import com.itqf.Service.User.UserService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.SuccessMessages;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.Note;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class NoteController {
@Resource
private NoteService noteService;
/*
根据token码查询我的笔记
李朝银
 */
    @GetMapping("huileme/a/u/NoteController/myNote")
    @ResponseBody
    public ResultDTO<List> findnote(HttpServletRequest request){
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid = getheader.getUserid();

        /*User user = userService.findbytoken(token);
        String userid = user.getUserid();*/
        List<Note> noteList = noteService.findbyuserid(userid);
        ResultDTO resultDTO=new ResultDTO();
        if (!noteList.isEmpty())
        {
            resultDTO.setData(noteList);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }

        return resultDTO;
}
/*
新增新的笔记
李朝银
 */
@PostMapping("huileme/a/u/NoteController/add")
@ResponseBody
public ResultDTO<User> addnote(HttpServletRequest request, String notetitle,String notecontent){


    RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
     User getheader = requestHeaderUtil.getheader(request);
     String userid = getheader.getUserid();

    /*User user = userService.findbytoken(token);
    String userid = user.getUserid();*/

    ResultDTO resultDTO=new ResultDTO();
    int i = noteService.addnote(userid,notetitle,notecontent);
    if (i>0){
       resultDTO.setMessage(CodeEnum.addsuccess.getMsg());
       resultDTO.setCode(CodeEnum.addsuccess.getCode());

    }else
    {
        resultDTO.setMessage(CodeEnum.adderror.getMsg());
        resultDTO.setCode(CodeEnum.adderror.getCode());
    }

    return resultDTO;
}

/*
笔记修改回显
李朝银
 */
@GetMapping("/huileme/a/u/NoteController/findbyid")
@ResponseBody
public ResultDTO<Note> findusernoteid(String usernoteid){
    Note note = noteService.findbyusernoteid(usernoteid);

    ResultDTO resultDTO=new ResultDTO();
    if (note!=null)
    {
        resultDTO.setData(note);
        resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
    }
    return resultDTO;
}

/*
修改笔记
李朝银
 */
    @PostMapping(value = "/huileme/a/u/NoteController/myNoteAddOrUpdate" ,produces ="application/json;charset=utf-8")
    @ResponseBody()
    public ResultDTO updatenote(String notetitle,String notecontent,Integer usernoteid){

       ResultDTO resultDTO=new ResultDTO();
        int i = noteService.updatenote(notetitle,notecontent,usernoteid);
        if (i>0){
            resultDTO.setMessage(CodeEnum.Updatesuccess.getMsg());
            resultDTO.setCode(CodeEnum.Updatesuccess.getCode());
            return resultDTO;
        }else
        {
            resultDTO.setMessage(CodeEnum.Updateerror.getMsg());
            resultDTO.setCode(CodeEnum.Updateerror.getCode());
        }

        return resultDTO;
    }
/*
删除笔记
李朝银
 */

    @DeleteMapping(value = "/huileme/a/u/NoteController/myNoteAddOrUpdate1" ,produces ="application/json;charset=utf-8")
    @ResponseBody()

    public ResultDTO delect(int usernoteid){
        ResultDTO resultDTO=new ResultDTO();
        int i = noteService.delect(usernoteid);
        if (i>0){
            resultDTO.setMessage(CodeEnum.deletesuccess.getMsg());
            resultDTO.setCode(CodeEnum.deletesuccess.getCode());
        }else
        {
            resultDTO.setMessage(CodeEnum.deleteerror.getMsg());
            resultDTO.setCode(CodeEnum.deleteerror.getCode());
        }

        return resultDTO;
    }
}
